package com.mach4.gamesuite;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
//import android.content.Intent;
//import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
//import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class BattleshipActivity extends Activity 
{
	ProgressDialog mProgressDialog;
	String playerIntent;
	
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        if(playerIntent == null)
        {
        	//Display Starting board.
        	setStartBoard();
        		
	        //Read intent of player
	        Bundle extras = getIntent().getExtras();
	        playerIntent = extras.getString("player_intent");
        }        
	    
		BattleshipManager.getInstance().setBoardUI(this);
		
		//turn off sleep mode during the game play so that we could keep internet connection
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }  
    
    /* Purpose: set the starting board where user places his/her ships
     * Pre: none.
     * Post: 10x10 grid of image buttons displayed with default water image.
     * 	 Each image is tagged with empty to signify it has no contents.
     */
    public void setStartBoard()
    {
    	//Display grid as described in battleship_start.xml file
    	setContentView(R.layout.battleship_start);
    	
    	for(int i = 0; i < 100; i++)
    	{
    		int id = getResources().getIdentifier("start"+i, "id", getPackageName());
    		ImageButton gridButton = (ImageButton)findViewById(id);
    		
    		//Set water image
    		gridButton.setImageResource(R.drawable.water);
    		
    		//Set contents to empty
    		gridButton.setTag(R.string.content, R.string.empty);
    	}
    }
    
    public void displayDialog()
    {
    	
    }
    public void initBoard(BattleshipManager manager)
    {
    	if (mProgressDialog != null)
    		mProgressDialog.dismiss();
    }
    
	public synchronized void showFinalDialog(String title, String message)
	{
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(title);
		alert.setMessage(message);
		alert.setNeutralButton("OK", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				finish();
			}
		});
		
		alert.show();
	}
    
	/** Begin Functions for initial ship placement set up *******************************/
	/* Purpose: Have user place his/her ships on the board
	 * Pre: none.
	 * Post: User's defend board updated with new ship locations.
	 * Return: String representing board state.
	 */
	private int shipCount = 0; //Track which ship is being placed
	private int placeEnd = 0; //Track which end is being placed. 0 = front, 1 = back
	private int frontEndPos;
	public void placeShipEnd(View v)
	{
		//Get position of button pushed. Tracked by its tag.
		String tag = v.getTag().toString();
		int pos = Integer.parseInt(tag);
		
		//Check which end of the ship is being placed
		if(placeEnd == 0) //Placing front end
		{
			placeFrontEnd(pos); //Place front end
		}
		else if(placeEnd == 1) //Placing back end
		{
			int id = getResources().getIdentifier("start"+pos, "id", getPackageName());
			ImageButton pressedButton = (ImageButton)findViewById(id);
			
			//Check content of button pushed
			if(pressedButton.getTag(R.string.content).equals(R.string.empty))
			{	//Button is empty. Previously placed front end should be moved to
				//  this new space.
				
				//Reset hints
				resetHints();
				
				//Reset old front end placement
				int oid = getResources().getIdentifier("start" + frontEndPos, "id", getPackageName());
				ImageButton oldFrontButton = (ImageButton)findViewById(oid);
				oldFrontButton.setTag(R.string.content, R.string.empty); //Set content to empty
				oldFrontButton.setImageResource(R.drawable.water); //Set image to water.
				
				//Move ship's front end
				placeFrontEnd(pos);
			}
			else if(pressedButton.getTag(R.string.content).equals(R.string.hint))
			{
				//Button contains one of the hints, which means other end of ship
				//  should be placed in this space.
				
				//Picked hint space, so fill in ship
				switch(shipCount)
				{
					case 0: fillShip(pos, 5); break; //Filling in carrier
					case 1: fillShip(pos, 4); break; //Filling in battleship
					case 2: fillShip(pos, 3); break; //Filling in Cruiser
					case 3: fillShip(pos, 3); break; //Filling in Submarine
					case 4: fillShip(pos, 2); break; //Filling in destroyer
				}
			}
		}
		else
		{
			Log.w("BattleshipActivity.placeShipEnd", "Invalid end of ship");
		}
	}
	
	/*
	 * Purpose: Place front end of a ship and place hints where other end of ship can go
	 * Parameters: ship tracks which ship is being placed.
	 * 		shipSize is the number of spaces occupied by the ship.
	 * 		pos is the position on the board where the front end is placed.
	 * Pre: pos is on the board (pos >= 0 && pos < 100)
	 * Post: Grid updated with end of ship and hints.
	 */
	private void placeFrontEnd(int pos)
	{
		//Find button where ship is to be placed
		int id = getResources().getIdentifier("start"+pos, "id", getPackageName());
		ImageButton pressedButton = (ImageButton)findViewById(id);
		
		//Set button to show where end of ship is
		switch(shipCount)
		{
			case 0: //placing carrier
				pressedButton.setImageResource(R.drawable.ship);
				placeHints(pos, 5); //Place hints
				break;
				
			case 1: //placing battleship
				pressedButton.setImageResource(R.drawable.ship);
				placeHints(pos, 4); //Place hints
				break;
				
			case 2: //placing cruiser
				pressedButton.setImageResource(R.drawable.ship);
				placeHints(pos, 3); //Place hints
				break;
				
			case 3: //placing submarine
				pressedButton.setImageResource(R.drawable.ship);
				placeHints(pos, 3); //Place hints
				break;
				
			case 4: //placing destroyer
				pressedButton.setImageResource(R.drawable.ship);
				placeHints(pos, 2); //Place hints
				break;
		}
		
		frontEndPos = pos; //Store front end position
		placeEnd = 1; //Placing second end of ship
	}
	
	/* Purpose: Place hints where a ship's back end can be placed.
	 * Parameters: pos is the position of the ship's front end.
	 * 		shipSize is the number of spaces occupied by the ship.
	 * Pre: pos is on the board.
	 * Post: Hint images placed on board where a ship's back end can be placed.
	 */
	private void placeHints(int pos, int shipSize)
	{
		//Ship size decremented to account for front end that was already placed.
		shipSize = shipSize - 1;
		
		//Place hint on vertical spaces
		for(int i = -10; i <= 10; i=i+20)
		{
			int hintDist = shipSize * i;
			int hintPos = pos + hintDist;
			
			//Check if hint space is on board
			if(hintPos >= 0 && hintPos < 100)
			{
				ImageButton pressedButton;
				
				//Check spaces to hint space and check they're empty
				boolean isEmpty = true;
				for(int j = 1; j <= shipSize && isEmpty; j++)
				{
					int id = getResources().getIdentifier("start" + (pos+(j * i)), "id", getPackageName());
					pressedButton = (ImageButton)findViewById(id);
					
					if(!pressedButton.getTag(R.string.content).equals(R.string.empty))
						isEmpty = false;
					
					//Place hint if spaces are empty
					if(j == shipSize && isEmpty)
					{
						//pressedButton.setText("X");
						pressedButton.setImageResource(R.drawable.ship);
						pressedButton.setTag(R.string.content, R.string.hint);
					}
				}
			}
		}
		
		//Place hint on horizontal spaces
		for(int i = -1; i <= 1; i = i + 2)
		{
			int hintDist = shipSize * i;
			int hintPos = pos + hintDist;
			
			//Check if hint space is same row
			if(hintPos >= (10 * (pos/10)) && hintPos < (10 * ((pos/10)+1)) )
			{
				ImageButton pressedButton;
				
				//Check spaces to hint spaces and check they're empty
				boolean isEmpty = true;
				for(int j = 1; j <= shipSize && isEmpty; j++)
				{
					int id = getResources().getIdentifier("start" + (pos+(j*i)), "id", getPackageName());
					pressedButton = (ImageButton)findViewById(id);
					
					if(!pressedButton.getTag(R.string.content).equals(R.string.empty))
						isEmpty = false;
					
					//Place hint if spaces are empty
					if(j == shipSize && isEmpty)
					{
						pressedButton.setImageResource(R.drawable.ship);
						pressedButton.setTag(R.string.content, R.string.hint);
					}
				}
			}
		}
	}
	
	/* Purpose: erase hints from board.
	 * Pre: none.
	 * Post: All buttons that have a hint are reset.
	 */
	private void resetHints()
	{
		for(int i = 0; i < 100; i++)
		{
			int id = getResources().getIdentifier("start"+i, "id", getPackageName());
			ImageButton tempButton = (ImageButton)findViewById(id);
			
			//Check if there is a hint in the button
			if(tempButton.getTag(R.string.content).equals(R.string.hint))
			{
				//Reset the space image to water
				tempButton.setImageResource(R.drawable.water);
				tempButton.setTag(R.string.content, R.string.empty);
			}
		}
	}
	
	/* Purpose: Place the ship's back end and fill in spaces in between.
	 * Parameters: pos is the position of the back end of ship.
	 * 		shipSize is the number of spaces occupied of the ship.
	 * Pre: pos is on the board.
	 * Post: Board has ship completely filled in.
	 */
	public void fillShip(int pos, int shipSize)
	{
		//Valid end position chosen. Fill in empty spaces
		//Find direction from front end to back end of ship
		int direction = ((pos) - frontEndPos)/(shipSize - 1);
							
		//Fill in remaining ship spaces
		for(int i = 0; i <= (shipSize-1); i++)
		{						
			int fid = getResources().getIdentifier("start" + ((pos) - i * direction), "id", getPackageName());
			ImageButton fillButton = (ImageButton)findViewById(fid);
			
			switch(shipCount)
			{
				//case 0:	fillButton.setText("" + BattleshipManager.getInstance().CARRIER); break;
				//case 1:	fillButton.setText("" + BattleshipManager.getInstance().BATTLESHIP); break;
				//case 2:	fillButton.setText("" + BattleshipManager.getInstance().CRUISER); break;
				//case 3: fillButton.setText("" + BattleshipManager.getInstance().SUBMARINE); break;
				//case 4: fillButton.setText("" + BattleshipManager.getInstance().DESTROYER); break;
				//default: break;
				case 0: //Filling in carrier
					fillButton.setImageResource(R.drawable.ship);
					fillButton.setTag(R.string.content, R.string.carrier);
					break;
				case 1: //Filling in battleship
					fillButton.setImageResource(R.drawable.ship);
					fillButton.setTag(R.string.content, R.string.battleship);
					break;
				case 2: //Filling in cruiser
					fillButton.setImageResource(R.drawable.ship);
					fillButton.setTag(R.string.content, R.string.cruiser);
					break;
				case 3:
					fillButton.setImageResource(R.drawable.ship);
					fillButton.setTag(R.string.content, R.string.submarine);
					break;
				case 4:
					fillButton.setImageResource(R.drawable.ship);
					fillButton.setTag(R.string.content, R.string.destroyer);
					break;
				default: break;
			}
		}
		
		//End of ship placed. Move on to next ship and reset end of ship tracker
		TextView temp = (TextView)findViewById(R.id.textView1);
		switch(shipCount)
		{
			case 0: temp.setText(R.string.addBattleship);break;						
			case 1: temp.setText(R.string.addCruiser);break;							
			case 2:temp.setText(R.string.addSubmarine);break;							
			case 3:temp.setText(R.string.addDestroyer);break;							
			case 4:temp.setText("Press Start");break;							
			default:break;
		}
		shipCount++;
		placeEnd = 0;
		resetHints();
	}
	
	/* Purpose: Start the game.
	 * Parameters: v is the view that is a reference to button that was pushed
	 * 		to call this function.
	 * Pre: All ships have been placed or Random placement chosen
	 * Post: Location of ships stored in appopriate board. Battleship Manager
	 * 	signalled to begin game.
	 */
	public void start(View v)
	{
		//Check if all ships have been placed yet
		if(shipCount < 5)
		{
			Toast toast = Toast.makeText(this, "All ships not placed yet", Toast.LENGTH_SHORT);
			toast.show();
		}
		else
		{
			//Store locations of ship in appropriate board
			setBoard();
			
			//Check if player's creating a game or joining one
			if(playerIntent.equals("create"))
			{
				//Player is creating a game
				BattleshipManager.getInstance().createGame();
				
				//Display dialog to ask user to wait until another player joins.
				mProgressDialog = ProgressDialog.show(this, "", "Waiting for opponent...", true, true,
					new OnCancelListener()
					{
						@Override
						public void onCancel(DialogInterface dialog)
						{
							BattleshipManager.getInstance().onBackKeyPress();
							finish();
						}
					}
				);
			}
			else
			{
				//Player is joining a game. playerIntent contains ID of game to join
				BattleshipManager.getInstance().joinGame(playerIntent);
			}
		}
	}
	
	/* Purpose: Randomize ship placement
	 * Parameters: v is the view that is a reference to button that was pushed
     *   to call this function.
	 * Pre: none
	 * Post: Board is cleared, then ships are placed randomly
	 */
	public void placeRandom(View v)
	{
	    Log.w("BattleshipActivity.placeRandom", "placeRandom called");
	    char[][] theBoard = new char[10][10]; //Not going to mess with buttons, temporary board
	    char shipSymbol;
	    Boolean placed, clear;
	    int x,y,xdir,ydir,length,tries,direction,step;
	    Random generator = new Random();
	    for(int row = 0; row < 10; row++)     //Clear the temporary board
	        for(int col = 0; col < 10; col++)
	            theBoard[row][col] = 'E';
	    shipCount = 0;
	    while(shipCount < 5)                  //While there are still ships to place
	    {
	        placed = false;
	        switch(shipCount)                 //Set symbol and length of ship being placed
	        {
	        case  0: length = 5; shipSymbol = 'C'; break;
	        case  1: length = 4; shipSymbol = 'B'; break;
	        case  2: length = 3; shipSymbol = 'K'; break;
	        case  3: length = 3; shipSymbol = 'S'; break;
	        case  4: length = 2; shipSymbol = 'D'; break;
	        default: length = 0; shipSymbol = 'E'; break; //Error
	        }
	        do
	        {
	            x = generator.nextInt(10);     //Generate random coordinates
	            y = generator.nextInt(10);
	        } while(theBoard[x][y] != 'E');    //Find empty spot
	        clear = true;                      //Keeps track of progress
	        direction = generator.nextInt(4);  //Pick a random direction
	        tries = 0;
	        while( tries < 4 && !placed )      //Try each direction before giving up
	        {
    	        switch(direction)              //Use direction to get coordinate deltas
    	        {
    	        default:
    	        case 0: xdir =  1; ydir =  0; break; //Right
    	        case 1: xdir = -1; ydir =  0; break; //Left
    	        case 2: xdir =  0; ydir =  1; break; //Up
    	        case 3: xdir =  0; ydir = -1; break; //Down
    	        }
    	        for(step = 1; step < length && clear; step++) //Check "length" spots (x,y) was already checked
    	            if(x+step*xdir < 0 || x+step*xdir > 9 || y+step*ydir < 0
    	                    || y+step*ydir > 9 || theBoard[x+step*xdir][y+step*ydir] != 'E')
    	                clear = false; //Ran off the board, or into another ship, direction won't work
    	        if(clear)              //Didn't run into anything, so go ahead
    	        {
    	            for(step = 0; step < length; step++)
    	                theBoard[x+step*xdir][y+step*ydir] = shipSymbol;
    	            placed = true;
    	            shipCount++;
    	        }
    	        else                   //Placement didn't work, try next direction
    	        {
    	            tries++;
    	            direction = (direction+1)%4;
    	        }    	        
	        }
	    } //Ships are placed
	    String boardState = ""; //Turn theBoard into a boardState
	    for(int row = 0; row < 10; row++)
	        for(int col = 0; col < 10; col++)
	            boardState += theBoard[row][col];
	    BattleshipManager.getInstance().setBoard(boardState); //Setup the board in the Manager
        //Check if player's creating a game or joining one
        if(playerIntent.equals("create"))
        {
            //Player is creating a game
            BattleshipManager.getInstance().createGame();
            
            //Display dialog to ask user to wait until another player joins.
            mProgressDialog = ProgressDialog.show(this, "", "Waiting for opponent...", true, true,
                new OnCancelListener()
                {
                    @Override
                    public void onCancel(DialogInterface dialog)
                    {
                        BattleshipManager.getInstance().onBackKeyPress();
                        finish();
                    }
                }
            );
        }
        else
        {
            //Player is joining a game. playerIntent contains ID of game to join
            BattleshipManager.getInstance().joinGame(playerIntent);
        }
	}
	
	/* Purpose: Store user's ship placement in user's defend board
	 * Pre: all ships placed.
	 * Post: Battleship Manager's defend board contains location of ships.
	 */
	private void setBoard()
	{
		String boardState = "";
		
		//Get user's ship placements
		for(int row = 0; row < 10; row++)
		{
			for(int col = 0; col < 10; col++)
			{
				int id = getResources().getIdentifier("start" + ((row * 10)+col), "id", getPackageName());
				ImageButton pressedButton = (ImageButton)findViewById(id);
				
				//Read contents of the button and append boardState appropriately
				if(pressedButton.getTag(R.string.content).equals(R.string.carrier))
					boardState += "C";
				else if(pressedButton.getTag(R.string.content).equals(R.string.battleship))
					boardState += "B";
				else if(pressedButton.getTag(R.string.content).equals(R.string.cruiser))
					boardState += "K";
				else if(pressedButton.getTag(R.string.content).equals(R.string.submarine))
					boardState += "S";
				else if(pressedButton.getTag(R.string.content).equals(R.string.destroyer))
					boardState += "D";
				else
					boardState += "E";
			}
		}
		
		BattleshipManager.getInstance().setBoard(boardState);
	}
	/** End Functions for initial ship placement set up ************************************/
	
	
	/**************************************************************************************/
	/** Begin Functions for move handling *************************************************/
	/**************************************************************************************/
	/* Purpose: Highlight space for potential attack.
	 * Parameters: v is the view reference to the button that was pressed to call this.
	 * Pre: none.
	 * Post: Potential attack space is highlighted.
	 * 		Coordinates of potential attack loaded into appropriate text box.
	 */
    public void onButtonPressed(View v)
    { 
    	ImageButton button = (ImageButton)v;
    	
    	//Reset other highlights
    	for(int i = 1; i <= 100; i++)
    	{
    		int id = getResources().getIdentifier("attack"+i, "id", getPackageName());
    		ImageButton resetButton = (ImageButton)findViewById(id);
    		
    		if(resetButton.getTag(R.string.content).equals(R.string.highlight))
    		{
    			resetButton.setTag(R.string.content, R.string.empty);
    			resetButton.setImageResource(R.drawable.water);
    		}
    	}
    	
    	//Get position of potential attack space
    	int buttonId = Integer.parseInt((String)button.getTag());
    	int buttonRow = buttonId/10;
    	int buttonCol = buttonId%10;
    	
    	//Check contents of potential attack
    	if(button.getTag(R.string.content).equals(R.string.empty))
    	{
    		//Highlight space
    		button.setImageResource(R.drawable.water_highlight);
    		button.setTag(R.string.content, R.string.highlight);
    		
    		//Update coordinate boxes
    		EditText coordBox = (EditText)findViewById(R.id.X); //X coordinate
    		coordBox.setText(Integer.toString(buttonCol+1));
    		
    		coordBox = (EditText)findViewById(R.id.Y); //Y coordinate
    		coordBox.setText(Integer.toString(buttonRow + 1));
    	}
    	else
    	{
    		//Chose spot that was already attacked.
			Toast toast = Toast.makeText(this, "Spot already attacked", Toast.LENGTH_SHORT);
			toast.show();
    	}
    }
    
    /* Purpose: Display user's attack board
     * Pre: none.
     * Post: User's hits, misses and empty spaces printed to screen.
     */
    public void displayAttackBoard()
	{
		char [][] attackBoard = BattleshipManager.getInstance().getAttack();
		
		for(int i = 0; i < 100; i++)
		{
			int id = getResources().getIdentifier("attack"+(i+1), "id", getPackageName());
			ImageButton setButton = (ImageButton)findViewById(id);
			
			if(!setButton.isEnabled())
				setButton.setEnabled(true);
			
			//Display hits, misses and empty spaces
			if(attackBoard[i/10][i%10] == BattleshipManager.getInstance().HIT)
			{
				setButton.setImageResource(R.drawable.hit);
				setButton.setTag(R.string.content, R.string.hit);
			}
			else if(attackBoard[i/10][i%10] == BattleshipManager.getInstance().MISS)
			{
				setButton.setImageResource(R.drawable.miss);
				setButton.setTag(R.string.content, R.string.miss);
			}
			else
			{
				setButton.setImageResource(R.drawable.water);
				setButton.setTag(R.string.content, R.string.empty);
			}
		}
		
		//Display submission buttons.
    	RelativeLayout submitMenu = (RelativeLayout)findViewById(R.id.submitLayout);
    	submitMenu.setVisibility(submitMenu.VISIBLE);

    	//Display heading to show which board is displayed.
		TextView temp = (TextView)findViewById(R.id.textView1);
		temp.setText("ATTACK!");
	}
    
    /* Purpose: Display user's defend board (board with the ships).
     * Pre: none.
     * Post: Location of ships, opponent's hits&misses displayed on screen.
     */
    public void displayDefendBoard()
    {
    	setContentView(R.layout.battleship_board);
    	char [][] defendBoard = BattleshipManager.getInstance().getDefend();
    	
    	for(int i = 0; i < 100; i++)
    	{
    		int id = getResources().getIdentifier("attack"+(i+1), "id", getPackageName());
    		ImageButton defendSpace = (ImageButton)findViewById(id);
    		defendSpace.setEnabled(false);
    		
    		//Display opponent's hits and misses and user's ships and empty spaces.
    		if(defendBoard[i/10][i%10] == BattleshipManager.getInstance().HIT)
    			defendSpace.setImageResource(R.drawable.hit);
    		else if(defendBoard[i/10][i%10] == BattleshipManager.getInstance().MISS)
    			defendSpace.setImageResource(R.drawable.miss);
    		else if(defendBoard[i/10][i%10] == BattleshipManager.getInstance().EMPTY)
    			defendSpace.setImageResource(R.drawable.water);
    		else
    			defendSpace.setImageResource(R.drawable.ship);
    	}
    	
    	//Hide submit buttons so that user doesn't accidentally submit a move
    	//  while waiting for opponent.
    	RelativeLayout submitMenu = (RelativeLayout)findViewById(R.id.submitLayout);
    	submitMenu.setVisibility(submitMenu.GONE);

    	//Change heading to show which board is displayed.
		TextView temp = (TextView)findViewById(R.id.textView1);
		temp.setText("Waiting for opponent.  Your ships:");
    }
	
	/* Purpose: Read user's potential attack coordinates
	 * Parameters: v is a reference to the submit button
	 * Pre: coordinates of attack are valid
	 * Post: If coordinates are valid, they are sent to battleship manager
	 * 		to handle the attack.
	 */
	public void sendAttack(View v) {
		
		//Get String value of x coordinate of attack
		EditText xField = (EditText)findViewById(R.id.X);
		String xString = xField.getText().toString();
		
		//Get String value of y coordinate of attack
		EditText yField = (EditText)findViewById(R.id.Y);
		String yString = yField.getText().toString();
		
		//Check to make sure coordinates have been input
		if(xString.length() == 0 || yString.length() == 0)
		{
			Toast toast = Toast.makeText(this, "No Space Chosen", Toast.LENGTH_SHORT);
			toast.show();
		}
		else
		{
			//Convert string objects to integer
			int x = Integer.parseInt(xString);
			int y = Integer.parseInt(yString);
			
			//Check for valid attack coordinates
			if(x < 1 || x > 10 || y < 1 || y > 10)
			{
				Toast toast = Toast.makeText(this, "Invalid Coordinates", Toast.LENGTH_SHORT);
				toast.show();
			}
			else
			{
				//Coordinates are valid, so handle move
				BattleshipManager.getInstance().handleMyMove(y-1, x-1);
			}
		}
	}
	
	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{   
		if (keyCode != KeyEvent.KEYCODE_BACK) 
			return false;
		
		BattleshipManager.getInstance().onBackKeyPress();
		return true;	
	} 
	
	@Override
	public void onBackPressed() {
		BattleshipManager.getInstance().onBackKeyPress();
		super.onBackPressed();
	}
}
